1776F - Train Splitting - CodeForces Solution


constructive algorithms graphs greedy *1700

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(v) begin(v), end(v)
#define ff first
#define ss second
#define sz(x) ((int)x.size())
const char nl = '\n';

template<class t, class u> bool pmin(t &a, u b) {if (b < a) {a = b; return true;} else return false;}
template<class t, class u> bool pmax(t &a, u b) {if (a < b) {a = b; return true;} else return false;}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int __ = 1;
    cin >> __;
    while (__--) {
        int n, m;
        cin >> n >> m;
        vector<vector<pair<int, int> > > g(n);
        int grid[n][n] = {};
        for (int i = 0; i < m; i++) {
            int u, v;
            cin >> u >> v;
            --u, --v;
            grid[u][v] = i + 1;
            grid[v][u] = i + 1;
            g[u].push_back({v, i + 1});
            g[v].push_back({u, i + 1});
        }
        if (n == 3) {
            if (m == 2) {
                cout << 2 << nl << 1 << " " << 2 << nl;
            }
            else {
                cout << 3 << nl << 1 << " " << 2 << " " << 3 << nl;
            }
            continue;
        }
        vector<int> ans(m + 1);
        int num;
        if (m == n * (n - 1) / 2) {
            num = 3;
            ans[grid[0][1]] = 1;
            ans[grid[0][n - 1]] = 1;
            for (int i = 2; i < n - 1; i++) {
                ans[grid[0][i]] = 3;
            }
        }
        else {
            num = 2;
            int node = 0;
            for (; node < n - 1; node++) {
                if (sz(g[node]) < n - 1) {
                    break;
                }
            }
            for (auto &v : g[node]) {
                ans[v.ss] = 1;
            }
        }
        cout << num << nl;
        for (int i = 1; i <= m; i++) {
            if (ans[i] == 0) {
                ans[i] = 2;
            }
            cout << ans[i] << " ";
        }
        cout << nl;
    }
}


Comments

Submit
0 Comments
More Questions

361A - Levko and Table
5E - Bindian Signalizing
687A - NP-Hard Problem
1542C - Strange Function
961E - Tufurama
129D - String
888A - Local Extrema
722B - Verse Pattern
278A - Circle Line
940A - Points on the line
1742C - Stripes
1742F - Smaller
1742B - Increasing
1742A - Sum
1742D - Coprime
390A - Inna and Alarm Clock
1398B - Substring Removal Game
1742G - Orray
416B - Art Union
962A - Equator
803B - Distances to Zero
291A - Spyke Talks
1742E - Scuza
1506D - Epic Transformation
1354G - Find a Gift
1426F - Number of Subsequences
1146B - Hate "A"
1718C - Tonya and Burenka-179
834A - The Useless Toy
1407D - Discrete Centrifugal Jumps